#include "widget.h"
#include "ui_widget.h"

#include <QDebug>

#include <RRT_Type.h>

Q_DECLARE_METATYPE(QVector<QPoint>);
Q_DECLARE_METATYPE(QPoint);
Q_DECLARE_METATYPE(TNode);

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    qRegisterMetaType<QVector<QPoint>>("QVector<QPoint>");
    qRegisterMetaType<QPoint>("QPoint");
    qRegisterMetaType<TNode>("TNode");

    this->setFixedSize(1260, 1070);
    this->setWindowTitle(QStringLiteral("路径规划可视化-RRT"));

    m_timer = new QTimer();
    m_timer->setInterval(100);
    m_timer->setSingleShot(true);

    connect(m_timer, &QTimer::timeout, this, &Widget::timeOutCallback);

    createAboutThread();

    connect(ui->widget_mapShow, &CDisplayWidget::signalNotifyMapSize, m_worker, &CWorker::slotUpdateGridMapSize);
    connect(ui->widget_mapShow, &CDisplayWidget::signalUpdateMapData, m_worker, &CWorker::slotUpdateGridMapData);

    connect(ui->widget_mapShow, &CDisplayWidget::signalSetStartPoint, m_worker, &CWorker::slotSetStartPoint);
    connect(ui->widget_mapShow, &CDisplayWidget::signalSetDestPoint, m_worker, &CWorker::slotSetDestPoint);

    connect(ui->widget_mapShow, &CDisplayWidget::signalNotifyMapSize, this, &Widget::slotUpdateGridMapSize);
    connect(this, &Widget::signalInitDisplayWidgetMode, ui->widget_mapShow, &CDisplayWidget::slotInitMode);
    connect(this, &Widget::signalClearDisplayWidgetResult, ui->widget_mapShow, &CDisplayWidget::slotClearPath);
    connect(this, &Widget::signalResetDisplayWidgetMap, ui->widget_mapShow, &CDisplayWidget::slotResetMap);
    connect(this, &Widget::signalResetDisplayWidgetMap, m_worker, &CWorker::slotResetGridMapData);

    connect(this, &Widget::signalStartPlanning, m_worker, &CWorker::slotStartPlanning);

    connect(m_worker, &CWorker::signalDisplayRandomPoint, ui->widget_mapShow, &CDisplayWidget::slotDisplayRandomPoint);
    connect(m_worker, &CWorker::signalDisplayTreeNode, ui->widget_mapShow, &CDisplayWidget::slotReceiveNewTreePoint);
    connect(m_worker, &CWorker::signalDisplayOriginPath, ui->widget_mapShow, &CDisplayWidget::slotReceiveOriginPath);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::createAboutThread()
{
    m_worker = new CWorker;
    m_thread = new QThread;
    m_worker->moveToThread(m_thread);

    connect(m_thread, &QThread::started, m_worker, &CWorker::createObject);

    m_thread->setObjectName("WorkThread");
    m_thread->start();
}

void Widget::sendSignalToSetDisplayWidgetMode()
{
    int mouse_event_index = ui->comboBox->currentIndex();
    int obstacle_size = ui->spinBox_obsSize->value();
    emit signalInitDisplayWidgetMode(mouse_event_index, obstacle_size);
}

void Widget::timeOutCallback()
{
    qDebug("[Widget] timeOutCallback enter\n");
    sendSignalToSetDisplayWidgetMode();
}

void Widget::slotUpdateGridMapSize(quint16 height, quint16 width)
{
    Q_UNUSED(height);
    Q_UNUSED(width);

    m_timer->start();
}


void Widget::on_horizontalSlider_obsSize_valueChanged(int value)
{
    ui->spinBox_obsSize->setValue(value);

    sendSignalToSetDisplayWidgetMode();
}


void Widget::on_spinBox_obsSize_valueChanged(int arg1)
{
    ui->horizontalSlider_obsSize->setValue(arg1);

    sendSignalToSetDisplayWidgetMode();
}


void Widget::on_comboBox_currentIndexChanged(int index)
{
    Q_UNUSED(index);
    sendSignalToSetDisplayWidgetMode();
}


void Widget::on_pushButton_clearResult_clicked()
{
    emit signalClearDisplayWidgetResult();
}


void Widget::on_pushButton_resetMap_clicked()
{
    emit signalResetDisplayWidgetMap();
}


void Widget::on_pushButton_startPlan_clicked()
{
    qDebug("[Widget] click start plan\n");
    emit signalStartPlanning();

}

